﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SCRUM.Core.MembershipProviders;

namespace SCRUM.Core.BusinessObjects.GridLayout
{

    public class QueryBuilder
    {

        public string queryId { get; set; }
        public SortFilterAndPage dataView { get; set; }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="queryId">ID of initial query</param>
        /// <param name="view">View options for this page</param>
        public QueryBuilder(string queryId, SortFilterAndPage view)
        {
            this.queryId = queryId;
            this.dataView = view;
        }

        /// <summary>
        /// Method to create complete query with sorting, filterring and paging
        /// </summary>
        /// <returns>Final query</returns>
        public string CreateQuery()
        {
            string query = CommandResources.GetCommand(queryId);
            string filtering = createFilters();
            string sorting = createSorting();
            string paging = createPaging();

            string result = "";

            result = "SELECT * FROM ("
                     + query
                     + " )"
                     + filtering 
                     + sorting;

            return result;
        }

        /// <summary>
        /// Method to craete filterting part of query 
        /// </summary>
        /// <returns>Filterring statement of query</returns>
        private string createFilters()
        {
            string result = "";
            if (dataView.Filterring != null && dataView.Filterring.Count > 0)
            {
                result = "WHERE ";
                for (int i = 0; i < dataView.Filterring.Count; i++)
                {
                    result += dataView.Filterring[i].filterColumn + " = '" +
                        dataView.Filterring[i].filterValue + "' " + dataView.Filterring[i].logicalConnection;
                }
            }
            return result;
        }

        /// <summary>
        /// Method to add sorting to query 
        /// </summary>
        /// <returns>Query with sorting</returns>
        private string createSorting()
        {
            string result = "";
            if (dataView.Sorting == null)
            {
                dataView.Sorting = new SortOption("ID", true);
            }
            string order = "";
            if (dataView.Sorting.isAsc)
            {
                 order = "ASC";
            }
            else
            {
                order = "DESC";
            }
            result = " ORDER BY " + dataView.Sorting.columnName + " " + order;    
            return result;
        }

        /// <summary>
        /// Method to add paging to query 
        /// </summary>
        /// <param name="query">Inition query</param>
        /// <returns>Query with paging</returns>
        private string createPaging()
        {
            string result = "";


            return result;
        }

    }
}
